﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SmartAnnotation
{

    class RectangleType : AnnotationType
    {
        //*****************************************************
        // Private data members
        //*****************************************************
        private int id;
        private int xOrigin;
        private int yOrigin;
        private int width;
        private int height;
        private string title;
        private string text;
        private bool editing;
        private int hScroll;
        private int vScroll;

        private const int MIN_WIDTH = 10;
        private const int MIN_HEIGHT = 10;

        //*****************************************************
        // Constructor(s)
        //*****************************************************

        public RectangleType(int xOrigin, int yOrigin, int width, int height, string title, string text)
        {
            this.id = 0;
            this.xOrigin = xOrigin;
            this.yOrigin = yOrigin;
            this.width = width;
            this.height = height;
            this.title = title;
            this.text = text;
            this.editing = false;
        }

        public RectangleType(int id, int xOrigin, int yOrigin, int width, int height, string title, string text)
        {
            this.id = id;
            this.xOrigin = xOrigin;
            this.yOrigin = yOrigin;
            this.width = width;
            this.height = height;
            this.title = title;
            this.text = text;
            this.editing = false;
        }

        //*****************************************************
        // Getters & Setters
        //*****************************************************
        #region Getters and Setters
        public int GetID()
        {
            return id;
        }

        public int GetX()
        {
            return xOrigin;
        }

        public int GetY()
        {
            return yOrigin;
        }

        public int GetWidth()
        {
            return width;
        }

        public int GetHeight()
        {
            return height;
        }

        public string GetTitle()
        {
            return title;
        }

        public string GetText()
        {
            return text;
        }

        public int GetMinWidth()
        {
            return MIN_WIDTH;
        }

        public int GetMinHeight()
        {
            return MIN_HEIGHT;
        }

        public void SetTitle(string title)
        {
            this.title = title;
        }

        public void SetText(string text)
        {
            this.text = text;
        }
        #endregion
        //*****************************************************
        // Public Methods
        //*****************************************************
        #region Public Methods
        public void UpdateScroll(int hScrollBar, int vScrollBar)
        {
            hScroll = hScrollBar;
            vScroll = vScrollBar;
        }

        public void Draw(Graphics g, bool hover)
        {
            if (hover)
            {
                g.DrawRectangle(Pens.DarkBlue,
                                xOrigin - hScroll,
                                yOrigin - vScroll,
                                width,
                                height);
                g.DrawRectangle(Pens.LightBlue,
                                xOrigin + 1 - hScroll,
                                yOrigin + 1 - vScroll,
                                width - 2,
                                height - 2);
            }
            else
            {
                Pen outPen = new Pen(Color.FromArgb(150, Color.Black), 1f);
                Pen inPen = new Pen(Color.FromArgb(200, Color.White), 1f);

                g.DrawRectangle(outPen,
                                xOrigin - hScroll,
                                yOrigin - vScroll,
                                width,
                                height);
                g.DrawRectangle(inPen,
                                xOrigin + 1 - hScroll,
                                yOrigin + 1 - vScroll,
                                width - 2,
                                height - 2);
            }
        }

        public void SetEditing(bool value)
        {
            editing = value;
        }

        public bool IsEditing()
        {
            return editing;
        }

        public void Move(int deltaX, int deltaY)
        {
            if (editing)
            {
                xOrigin += deltaX;
                yOrigin += deltaY;
            }
        }

        public void Resize(int deltaX, int deltaY)
        {
            if (editing)
                if ((width + deltaX > MIN_WIDTH) && (height + deltaY > MIN_HEIGHT))
                {
                    width += deltaX;
                    height += deltaY;
                }
        }

        public bool IsInMiddle(int _x, int _y)
        {
            int x = _x + hScroll;
            int y = _y + vScroll;

            if (x > GetX() + 4 &&
                x < GetX() + GetWidth() - 4 &&
                y > GetY() + 4 &&
                y < GetY() + GetHeight() - 4)
                return true;
            else
                return false;
        }

        public bool IsInTopBorder(int _x, int _y)
        {
            int x = _x + hScroll;
            int y = _y + vScroll;

            if (y > (GetY() - 3) && y < (GetY() + 4) &&
                x > (GetX() - 3) && x < (GetX() + GetWidth() + 3))
                return true;
            else
                return false;
        }

        public bool IsInBottomBorder(int _x, int _y)
        {
            int x = _x + hScroll;
            int y = _y + vScroll;

            if (y > (GetY() + GetHeight() - 4) && y < (GetY() + GetHeight() + 3) &&
                x > (GetX() - 3) && x < (GetX() + GetWidth() + 3))
                return true;
            else
                return false;
        }

        public bool IsInLeftBorder(int _x, int _y)
        {
            int x = _x + hScroll;
            int y = _y + vScroll;

            if (x > (GetX() - 3) && x < (GetX() + 4) &&
                y > (GetY() - 3) && y < (GetY() + GetHeight() + 3))
                return true;
            else
                return false;
        }

        public bool IsInRightBorder(int _x, int _y)
        {
            int x = _x + hScroll;
            int y = _y + vScroll;

            if (x > (GetX() + GetWidth() - 4) && x < (GetX() + GetWidth() + 3) &&
                y > (GetY() - 3) && y < (GetY() + GetHeight() + 3))
                return true;
            else
                return false;
        }
        #endregion
    }
}